' Written by Craig'n'Dave
Module Module1
    ' Stack using objects
    Public Class Stack

        Private Class Node
            Public data As String
            Public pointer As Node
        End Class

        Private stack_pointer As Node

        Function push(item As String)
            ' Check memory overflow
            Try
                ' Push the item
                Dim new_node As New Node
                new_node.data = item
                new_node.pointer = stack_pointer
                stack_pointer = new_node
                Return True
            Catch
                Return False
            End Try
        End Function

        Function pop()
            ' Check stack underflow
            If Not IsNothing(stack_pointer) Then
                ' Pop the item
                Dim item As String = stack_pointer.data
                stack_pointer = stack_pointer.pointer
                Return item
            Else
                Return Nothing
            End If
        End Function

        Function peek()
            ' Check stack underflow
            If Not IsNothing(stack_pointer) Then
                ' Peek the item
                Return stack_pointer.data
            Else
                Return Nothing
            End If
        End Function
    End Class

    Sub main()
        Dim items() As String = {"Florida", "Georgia", "Delaware", "Alabama", "California"}
        Dim s As New Stack
        ' Add items to the stack
        For index = 0 To items.Length - 1
            s.push(items(index))
        Next
        'Remove items from the stack
        Console.WriteLine(s.pop)
        ' Output the next item in the stack
        Console.WriteLine(s.peek)
    End Sub
End Module
